package com.ebupt.migu.music.api.service.impl;

import com.ebupt.migu.music.api.mapper.RefreshUserGroupSizeMapper;
import com.ebupt.migu.music.api.service.RefreshUserGroupSizeService;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.StatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: liuyangyang@cd.ebupt.com
 * @create: 2020-11-18 17:22
 */
@Slf4j
@Service
public class RefreshUserGroupSizeServiceImpl implements RefreshUserGroupSizeService {

    @Autowired
    private RefreshUserGroupSizeMapper refreshUserGroupSizeMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 同步Redis中的人群规模到MySQL数据库
     *
     * @return
     */
    @Override
    public ResultObject refresh_user_group_size() {
        ResultObject res = new ResultObject();
        List<String> gids = refreshUserGroupSizeMapper.group_ids_to_refresh();
        if (null != gids && gids.size() != 0) {
            for (String gid : gids) {
                log.info("当前刷新的人群的人群ID为：{}", gid);
                // redis key
                String _phone = gid + "-phone";
                String _imei = gid + "-imei";
                String _uid = gid + "-uid";
                // 计算人群规模
                Map<String, Object> map = new HashMap<>();
                int user_group_size = 0;
                // uid 规模
                int client_user = 0;
                // 手机号码规模
                int mobile_user = 0;
                // 设备号规模
                int device_user = 0;

                if (redisTemplate.hasKey(_uid)) {
                    client_user = redisTemplate.opsForSet().size(_uid).intValue();
                    log.info("client_user：{}", client_user);
                }
                if (redisTemplate.hasKey(_imei)) {
                    device_user = redisTemplate.opsForSet().size(_imei).intValue();
                    log.info("device_user：{}", device_user);
                }
                if (redisTemplate.hasKey(_phone)) {
                    mobile_user = redisTemplate.opsForSet().size(_phone).intValue();
                    log.info("mobile_user：{}", mobile_user);
                }
                user_group_size = maxValue(client_user, mobile_user, device_user);
                log.info("user_group_size：{}", user_group_size);
                map.put("clientUser", client_user);
                map.put("mobileUser", mobile_user);
                map.put("deviceUser", device_user);
                map.put("userGroupSize", user_group_size);
                // 更新
                refreshUserGroupSizeMapper.refresh_user_group_size(map);
            }
            log.info("同步完成，共同步{}个人群", gids.size());
        } else {
            log.info("没有待刷新的人群");
        }

        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg(StatusEnum.SUCCESS.getMsg());
        return res;
    }

    /**
     * 返回abc三者最大值
     *
     * @param a
     * @param b
     * @param c
     * @return
     */
    public static int maxValue(int a, int b, int c) {
        int temp = a > b ? a : b;
        return temp > c ? temp : c;
    }
}
